
package com.yuan.adapt;

import com.yuan.graduate.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

public class RadioAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private int num;
    private List<?> list;
    private String key, pos = "";

    /**
     * 带有单选框的adapt
     * 
     * @author yuanwei-iri
     * @param context
     * @param num 总数
     * @param list 数据
     * @param key 对应的key值
     * @param pos pos显示true 当pos为负数 则全部为false
     */
    public RadioAdapter(Context context, int num, List<?> list, String key, String pos)
    {
        mInflater = LayoutInflater.from(context);
        this.num = num;
        this.list = list;
        this.key = key;
        this.pos = pos;
    }

    @Override
    public int getCount() {
        return num;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 改为ViewHolder
     * 
     * @author yuanwei-iri 2014-03-05
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null || convertView.getTag() == null)
        {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.setting_radio,
                    null);
            holder.checkedtext = (CheckedTextView) convertView
                    .findViewById(R.id.checkedtext);
            holder.txt = ((TextView) convertView.findViewById(R.id.txt));
            holder.line = (TextView) convertView
                    .findViewById(R.id.setting_pref_entry_radio_line);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txt.setText((String) ((Map<?, ?>) list.get(position)).get(key));
        if ((((Map<?, ?>) list.get(position)).get(key).toString()).equals(pos))
        {
            holder.checkedtext.setChecked(true);
        }
        else
        {
            holder.checkedtext.setChecked(false);
        }

        if (position == getCount() - 1)
        {
            holder.line.setVisibility(View.GONE);
        }
        else
        {
            holder.line.setVisibility(View.VISIBLE);
        }
        return convertView;
    }

    class ViewHolder {
        CheckedTextView checkedtext;
        TextView txt;
        TextView line;
    }

};
